<?php
session_start();


function fixObject(&$object) {
    if (!is_object($object) && gettype($object) == 'object')
        return ($object = unserialize(serialize($object)));
    return $object;
}

///////////////////Xu Ly Random Tu/////////////
 
$ThreeWord = array();
$virtualWord1 = new Word();
$virtualWord2 = new Word();
$rightWord = new  Word();
$tempThreeWords = array();

$RightWords =  $_SESSION["RightWords"];
$VirtualWord = $_SESSION["VirtualWord"];
$Point = 0;
if(isset($_SESSION["Point"]))
{
$Point = $_SESSION["Point"];
}

$orderRightWord = array_rand($RightWords);
$rightWord = $RightWords[$orderRightWord];
fixObject($rightWord);



array_push($ThreeWord, $rightWord);

while (count($ThreeWord) < 2) {
    $virtualWord1 = $VirtualWord[array_rand($VirtualWord)];
    fixObject($virtualWord1);
    if ($virtualWord1->id != $rightWord->id) {
        array_push($ThreeWord, $virtualWord1);
    }
}

while (count($ThreeWord) < 3) {
    $virtualWord2 = $VirtualWord[array_rand($VirtualWord)];
    fixObject($virtualWord2);
    if (($virtualWord2->id != $rightWord->id) && ($virtualWord2->id != $virtualWord1->id)) {
        array_push($ThreeWord, $virtualWord2);
    }
}

$_SESSION["ThreeWord"] = $ThreeWord;



if(count($RightWords)==0)
{
    
$IDUserOnline = elgg_get_logged_in_user_guid();
$idLesson = (int) $_SESSION["idLesson"];
    $status = new Status();
    $status->SetStatusTest($IDUserOnline,$idLesson);
    ?>
<h1>Your Number of the right answer is: <?php echo $Point; ?>/<?php echo count($VirtualWord); ?></h1>

<?php
    unset ($_SESSION["Point"]);
}
else
{
unset($RightWords[$orderRightWord]);
$_SESSION["RightWords"] = $RightWords;

?>



<div class = "topic_video"> <!--khung ghi đề bài-->
    <div class="left_audio">
        <audio src="<?php echo elgg_get_site_url(); ?>mod/MyCourse/<?php echo $rightWord->audio; ?>" controls/>
    </div>

    <div class="right_audio"> <!--Phần Câu Trả Lời-->
<?php echo $rightWord->engName; ?></div>
</div> <!-- End khung ghi đề bài -->


<div id = "choose_right_word"> <!-- choose_right_word -->

    <table>
        <tr>
<?php
for ($i = 0; $i < 3; ++$i) {
    $tempWord = new Word();
                $order = array_rand($ThreeWord);
                $tempWord = $ThreeWord[$order];
                unset($ThreeWord[$order]);
                array_push($tempThreeWords, $tempWord);
    ?>



                <td style="width: 250px;"> 
                    <div   class="picture_box" ><a href="#" onclick='ClickPicture(<?php echo $tempWord->id; ?>);' ><img  id="new_word" src="<?php echo elgg_get_site_url(); ?>mod/MyCourse/<?php echo $tempWord->picture; ?>" alt="new world"/>
                        </a><p><?php echo $tempWord->vietName; ?></p>
                    </div>
                </td>

    <?php
}
?>
        </tr>
    </table>
</div> <!-- End choose_right_word -->


<a  href="#" onclick='ClickNext();' class="elgg-button elgg-button-submit" >Next</a>
<?php 

$_SESSION["tempThreeWords"]=$tempThreeWords;
}?>